package com;

import java.util.Observable;
import java.util.Observer;

import ComLayer.ComunicationLayerControler;
import ComLayer.ComunicationLayerInterface;
import MouseControl.MouseControler;
import MouseControl.MouseControlerInterface;

/**
 * Class MainControler
 */
public class MainControler implements Observer {

	//
	// Fields
	//

	private ComunicationLayerControler ComLayer;
	private MouseControler Mouse;
	private int mouse_server_status = 0; // (0 inactivo, 1 activo)
	private int com_server_status = 1; // (0 inactivo, 1 activo)

	//
	// Constructors
	//
	public MainControler(ComunicationLayerInterface clc,
			MouseControlerInterface mc) {
		this.setComLayer(new ComunicationLayerControler(clc));
		this.setMouse(new MouseControler(mc));
		ComLayer.getObservable().addObserver(this);
		ComLayer.setStatus(1);

	};

	//
	// Methods
	//

	//
	// Accessor methods
	//

	/**
	 * Set the value of ComLayer
	 *
	 * @param newVar
	 *            the new value of ComLayer
	 */
	public void setComLayer(ComunicationLayerControler newVar) {
		ComLayer = newVar;
	}

	/**
	 * Get the value of ComLayer
	 *
	 * @return the value of ComLayer
	 */
	public ComunicationLayerControler getComLayer() {
		return ComLayer;
	}

	/**
	 * Set the value of Mouse
	 *
	 * @param newVar
	 *            the new value of Mouse
	 */
	public void setMouse(MouseControler newVar) {
		Mouse = newVar;
		Mouse.addObserver(this);
	}

	/**
	 * Get the value of Mouse
	 *
	 * @return the value of Mouse
	 */
	public MouseControler getMouse() {
		return Mouse;
	}

	//
	// Other methods
	//

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		java.util.LinkedList lista = this.ComLayer.getListaacciones();
		java.util.LinkedList<util.DeviceInfo> devicelist = this.ComLayer
				.getDeviceList();

		while (0 < lista.size()) {
			System.out.println("MainControlerUpdate: " + lista.getFirst().toString() + "->" + devicelist.getFirst().getName());
			//System.out.println("*" + devicelist.getFirst().getInfo() + "*");
			String mensaje = lista.getFirst().toString();
			String[] mensajitos = mensaje.split(" ");
			lista.removeFirst();
			devicelist.removeFirst();
			if (mensajitos[0].equalsIgnoreCase("move")) {
				Mouse.moveCursor(Mouse.getXCursor()
						+ Integer.parseInt(mensajitos[1]), Mouse.getYCursor()
						+ Integer.parseInt(mensajitos[2]));
			}
			if (mensajitos[0].equalsIgnoreCase("scroll")) {
				Mouse.scroll(Integer.parseInt(mensajitos[1]));
			}
			if (mensajitos[0].equalsIgnoreCase("click")) {
				if (Integer.parseInt(mensajitos[1]) == 1) {
					Mouse.pressLeft();
					Mouse.releaseLeft();
				}
				if (Integer.parseInt(mensajitos[1]) == 2) {
					Mouse.pressMiddle();
					Mouse.releaseMiddle();
				}
				if (Integer.parseInt(mensajitos[1]) == 3) {
					Mouse.pressRight();
					Mouse.releaseRigth();
				}

			}
			if (mensajitos[0].equalsIgnoreCase("press")) {
				if (Integer.parseInt(mensajitos[1]) == 1) {
					Mouse.pressLeft();
				}
				if (Integer.parseInt(mensajitos[1]) == 2) {
					Mouse.pressMiddle();
				}
				if (Integer.parseInt(mensajitos[1]) == 3) {
					Mouse.pressRight();
				}

			}
			if (mensajitos[0].equalsIgnoreCase("release")) {
				if (Integer.parseInt(mensajitos[1]) == 1) {
					Mouse.releaseLeft();
				}
				if (Integer.parseInt(mensajitos[1]) == 2) {
					Mouse.releaseMiddle();
				}
				if (Integer.parseInt(mensajitos[1]) == 3) {
					Mouse.releaseRigth();
				}

			}

		}

	}

	public int getMouse_server_status() {
		return mouse_server_status;
	}

	public void setMouse_server_status(int mouseServerStatus) {
		if ((mouseServerStatus == 1) || (mouseServerStatus == 0))
			mouse_server_status = mouseServerStatus;
	}

	public int getCom_server_status() {
		return com_server_status;
	}

	public void setCom_server_status(int comServerStatus) {
		if ((comServerStatus == 1) || (comServerStatus == 0))
			com_server_status = comServerStatus;
	}

}
